// 分身类
class Clone {
    constructor(x, y, player) {
        this.x = x;
        this.y = y;
        this.player = player;
        this.size = GAME_CONFIG.PLAYER_SIZE * 0.8;
        this.speed = GAME_CONFIG.PLAYER_SPEED * 0.7;
        this.health = 50;
        this.maxHealth = 50;
        this.lifetime = 10000; // 10秒存在时间
        this.maxLifetime = this.lifetime;
        this.isDead = false;
        this.color = '#9370DB';
        
        // 攻击相关
        this.attackCooldown = 400; // 更快的攻击频率
        this.lastAttackTime = 0;
        this.attackRange = 180; // 更大的攻击范围
        this.damage = 30; // 更高的攻击伤害
        
        // 跟随玩家
        this.followDistance = 80;
        this.followSpeed = 0.1;
    }
    
    // 更新分身
    update(deltaTime, enemies) {
        if (this.isDead) return;
        
        this.lifetime -= deltaTime;
        if (this.lifetime <= 0) {
            this.die();
            return;
        }
        
        this.updateMovement(deltaTime);
        this.updateAttack(deltaTime, enemies);
    }
    
    // 更新移动
    updateMovement(deltaTime) {
        if (!this.player) return;
        
        // 计算到玩家的距离
        const distanceToPlayer = Utils.distance(this.x, this.y, this.player.x, this.player.y);
        
        // 如果距离太远，跟随玩家
        if (distanceToPlayer > this.followDistance) {
            const angle = Utils.angle(this.x, this.y, this.player.x, this.player.y);
            const targetX = this.player.x + Math.cos(angle) * this.followDistance;
            const targetY = this.player.y + Math.sin(angle) * this.followDistance;
            
            // 平滑移动到目标位置
            this.x += (targetX - this.x) * this.followSpeed;
            this.y += (targetY - this.y) * this.followSpeed;
            
            // 边界检查（限制在世界范围内）
            this.x = Utils.clamp(this.x, this.size, GAME_CONFIG.WORLD_WIDTH - this.size);
            this.y = Utils.clamp(this.y, this.size, GAME_CONFIG.WORLD_HEIGHT - this.size);
        }
    }
    
    // 更新攻击
    updateAttack(deltaTime, enemies) {
        this.lastAttackTime += deltaTime;
        
        if (this.lastAttackTime >= this.attackCooldown) {
            const nearestEnemy = this.findNearestEnemy(enemies);
            if (nearestEnemy && this.getDistanceToEnemy(nearestEnemy) <= this.attackRange) {
                this.attack(nearestEnemy);
                this.lastAttackTime = 0;
            }
        }
    }
    
    // 寻找最近的敌人
    findNearestEnemy(enemies) {
        let nearestEnemy = null;
        let nearestDistance = Infinity;
        
        enemies.forEach(enemy => {
            if (!enemy.isDead) {
                const distance = this.getDistanceToEnemy(enemy);
                if (distance < nearestDistance) {
                    nearestDistance = distance;
                    nearestEnemy = enemy;
                }
            }
        });
        
        return nearestEnemy;
    }
    
    // 获取到敌人的距离
    getDistanceToEnemy(enemy) {
        return Utils.distance(this.x, this.y, enemy.x, enemy.y);
    }
    
    // 攻击敌人
    attack(enemy) {
        enemy.takeDamage(this.damage);
        
        // 显示伤害数字
        if (window.uiManager) {
            window.uiManager.showDamageNumber(enemy.x, enemy.y - 30, this.damage);
        }
    }
    
    // 受到伤害
    takeDamage(damage) {
        this.health -= damage;
        this.health = Math.max(0, this.health);
        
        if (this.health <= 0) {
            this.die();
        }
    }
    
    // 死亡
    die() {
        this.isDead = true;
    }
    
    // 渲染分身
    render(ctx) {
        if (this.isDead) return;
        
        ctx.save();
        
        // 根据剩余生命周期调整透明度
        const alpha = this.lifetime / this.maxLifetime;
        ctx.globalAlpha = alpha;
        
        // 使用精灵渲染分身
        if (window.game && window.game.spriteManager) {
            window.game.spriteManager.drawSprite(ctx, 'clone_entity', this.x, this.y, this.size * 2, this.size * 2);
        } else {
            // 备用渲染（圆形）
            ctx.fillStyle = this.color;
            ctx.beginPath();
            ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
            ctx.fill();
            
            ctx.strokeStyle = '#ffffff';
            ctx.lineWidth = 2;
            ctx.stroke();
            
            // 绘制分身标识
            ctx.fillStyle = '#ffffff';
            ctx.font = '10px Arial';
            ctx.textAlign = 'center';
            ctx.fillText('C', this.x, this.y + 3);
        }
        
        // 绘制生命值条
        this.renderHealthBar(ctx);
        
        ctx.restore();
    }
    
    // 渲染生命值条
    renderHealthBar(ctx) {
        const barWidth = this.size * 2;
        const barHeight = 3;
        const barX = this.x - barWidth / 2;
        const barY = this.y - this.size - 8;
        
        // 背景
        ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
        ctx.fillRect(barX, barY, barWidth, barHeight);
        
        // 生命值
        const healthPercentage = this.health / this.maxHealth;
        const healthWidth = barWidth * healthPercentage;
        
        ctx.fillStyle = healthPercentage > 0.5 ? '#00ff00' : healthPercentage > 0.25 ? '#ffff00' : '#ff0000';
        ctx.fillRect(barX, barY, healthWidth, barHeight);
        
        // 边框
        ctx.strokeStyle = '#ffffff';
        ctx.lineWidth = 1;
        ctx.strokeRect(barX, barY, barWidth, barHeight);
    }
    
    // 检查与其他对象的碰撞
    collidesWith(other) {
        return Utils.circleCollision(
            this.x, this.y, this.size,
            other.x, other.y, other.size || other.radius || 0
        );
    }
}

// 分身管理器
class CloneManager {
    constructor(player) {
        this.player = player;
        this.clones = [];
        this.maxClones = 3;
    }
    
    // 添加分身
    addClone() {
        if (this.clones.length >= this.maxClones) return;
        
        // 在玩家附近生成分身
        const angle = Math.random() * Math.PI * 2;
        const distance = 50 + Math.random() * 30;
        const x = this.player.x + Math.cos(angle) * distance;
        const y = this.player.y + Math.sin(angle) * distance;
        
        const clone = new Clone(x, y, this.player);
        this.clones.push(clone);
    }
    
    // 更新所有分身
    update(deltaTime, enemies) {
        this.clones.forEach(clone => clone.update(deltaTime, enemies));
        this.cleanupDeadClones();
    }
    
    // 清理死亡的分身
    cleanupDeadClones() {
        this.clones = this.clones.filter(clone => !clone.isDead);
    }
    
    // 渲染所有分身
    render(ctx) {
        this.clones.forEach(clone => clone.render(ctx));
    }
    
    // 获取分身数量
    getCloneCount() {
        return this.clones.length;
    }
    
    // 重置分身管理器
    reset() {
        this.clones = [];
    }
} 